from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class TestOk(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://localhost/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_create_ok(self):
        driver = self.driver
        driver.get(self.base_url + "/php4dvd/")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("admin")
        driver.find_element_by_name("password").clear()
        driver.find_element_by_name("password").send_keys("admin")
        driver.find_element_by_name("submit").click()
        driver.find_element_by_css_selector("img[alt=\"Add movie\"]").click()
        driver.find_element_by_name("name").clear()
        driver.find_element_by_name("name").send_keys("Title_test")
        driver.find_element_by_name("aka").clear()
        driver.find_element_by_name("aka").send_keys("as selenium_test")
        driver.find_element_by_name("year").clear()
        driver.find_element_by_name("year").send_keys("1991")
        driver.find_element_by_name("duration").clear()
        driver.find_element_by_name("duration").send_keys("250")
        driver.find_element_by_name("rating").clear()
        driver.find_element_by_name("rating").send_keys("10")
        driver.find_element_by_name("trailer").clear()
        driver.find_element_by_name("trailer").send_keys("http://software-testing.ru")
        driver.find_element_by_name("notes").clear()
        driver.find_element_by_name("notes").send_keys("Personal_notes_test")
        driver.find_element_by_name("taglines").clear()
        driver.find_element_by_name("taglines").send_keys("Taglines_test")
        driver.find_element_by_name("plotoutline").clear()
        driver.find_element_by_name("plotoutline").send_keys("Plot_outline_test")
        driver.find_element_by_name("plots").clear()
        driver.find_element_by_name("plots").send_keys("Plots_test")
        driver.find_element_by_id("text_languages_0").clear()
        driver.find_element_by_id("text_languages_0").send_keys("Languages_test")
        driver.find_element_by_name("subtitles").clear()
        driver.find_element_by_name("subtitles").send_keys("Subtitles_test")
        driver.find_element_by_name("audio").clear()
        driver.find_element_by_name("audio").send_keys("Audio_test")
        driver.find_element_by_name("video").clear()
        driver.find_element_by_name("video").send_keys("Video_test")
        driver.find_element_by_name("country").clear()
        driver.find_element_by_name("country").send_keys("Country_test")
        driver.find_element_by_name("genres").clear()
        driver.find_element_by_name("genres").send_keys("Genres_test")
        driver.find_element_by_name("director").clear()
        driver.find_element_by_name("director").send_keys("Director_test")
        driver.find_element_by_name("writer").clear()
        driver.find_element_by_name("writer").send_keys("Writer_test")
        driver.find_element_by_name("producer").clear()
        driver.find_element_by_name("producer").send_keys("Producer_test")
        driver.find_element_by_name("music").clear()
        driver.find_element_by_name("music").send_keys("Music_test")
        driver.find_element_by_name("cast").clear()
        driver.find_element_by_name("cast").send_keys("Cast_test")
        driver.find_element_by_id("submit").click()
        try: self.assertEqual("Title_test (1991)", driver.find_element_by_css_selector("h2").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Rating: 10", driver.find_element_by_css_selector("div.rating").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Languages: Languages_test", driver.find_element_by_css_selector("div.maininfo_full > div.languages").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Subtitles: Subtitles_test", driver.find_element_by_css_selector("div.subtitles").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Country: Country_test", driver.find_element_by_css_selector("div.country").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Director: Director_test", driver.find_element_by_css_selector("div.director").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Writer: Writer_test", driver.find_element_by_css_selector("div.writer").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Producer: Producer_test", driver.find_element_by_css_selector("div.producer").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Music: Music_test", driver.find_element_by_css_selector("div.music").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Video: Video_test", driver.find_element_by_css_selector("div.video").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Audio: Audio_test", driver.find_element_by_css_selector("div.audio").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Personal notes\nPersonal_notes_test", driver.find_element_by_xpath("//div[@id='movie']/div[2]/div[14]").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Plot_outline_test", driver.find_element_by_css_selector("div.plotoutline > div").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Cast_test", driver.find_element_by_css_selector("div.first.column > ul > li").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertEqual("Plots_test", driver.find_element_by_css_selector("ol > li").text)
        except AssertionError as e: self.verificationErrors.append(str(e))

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
